package lium.lbs.lbsim.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import lium.lbs.lbsim.R;


/**
 * Created by JonZone on 2015/9/2.
 * E-mail:com
 */
public class IAlertDialog extends Dialog {
    private TextView messageView;
    private TextView titleView;
    private TextView positiveButton;
    private TextView negativeButton;
    private ImageView closeButton;
    private String negativeMsg;
    private String positiveMsg;
    private String title;
    private String message;
    private OnClickListener positiveOnClickListener;
    private OnClickListener negativeOnClickListener;
    private int type = 0;  //显示控制标记
    private int gravity; //显示位置 @Gravity....

    public enum LayoutStyle {

        /**
         * 白色背景。title和message以及确定取消按钮一体。
         */
        DEFAULT,
        THEME_WHITE_TITLE_SINGLEBUTTON,
        READALL,
        CLEARALL,
        CHANGEHEAD //更换头像弹窗
    }

    //提示对话框
    public IAlertDialog(Context context,int gravity) {
        this(context, LayoutStyle.DEFAULT,gravity);
    }

    public IAlertDialog(Context context, LayoutStyle theme,int gravity) {
        super(context, R.style.loading_dialog);
        this.gravity=gravity;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        switch (theme) {
            case DEFAULT:
                setContentView(R.layout.dialog_default);
                break;
            case THEME_WHITE_TITLE_SINGLEBUTTON:
                setContentView(R.layout.dialog_white_bg_title1);
                break;
            case READALL:
                setContentView(R.layout.dialog_readall_bg_title1);
                break;
            case CLEARALL:
                setContentView(R.layout.dialog_clearall_bg_title1);
                break;
            case CHANGEHEAD:
                type = 2;
                setContentView(R.layout.dialog_changehead_bg_title1);
                break;
            default:
                setContentView(R.layout.dialog_default);
                break;
        }
        messageView = (TextView) findViewById(R.id.tv_tips_dialog_tips);
        titleView = (TextView) findViewById(R.id.tv_tips_dialog_title);
        positiveButton = (TextView) findViewById(R.id.tv_tips_dialog_confirm);

        negativeButton = (TextView) findViewById(R.id.tv_tips_dialog_cancel);
        positiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (positiveOnClickListener != null) {
                    positiveOnClickListener.onClick(IAlertDialog.this, v.getId());
                }
                dismiss();
            }
        });
        negativeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (negativeOnClickListener != null) {
                    negativeOnClickListener.onClick(IAlertDialog.this, v.getId());
                }
                dismiss();

            }
        });

        setCanceledOnTouchOutside(false);
        setCancelable(false);

        titleView.setText(title == null ? getContext().getString(R.string.promot) : title);
        messageView.setText(message == null ? "" : message);
        negativeButton.setText(negativeMsg == null ? getContext().getString(R.string.cancel) : negativeMsg);
        positiveButton.setText(positiveMsg == null ? getContext().getString(R.string.confirm) : positiveMsg);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    /**
     * 设置标题
     *
     * @param title
     */
    public IAlertDialog setTitle(String title) {
        this.title = title;
        if (titleView != null && title != null) {
            titleView.setText(title);
        }
        if (title == null && titleView != null) {
            titleView.setVisibility(View.GONE);
        }
        return this;
    }

    public TextView getTitleView() {
        return titleView;
    }

    public TextView getMessageView() {
        return messageView;
    }

    /**
     * 获取右侧按钮
     *
     * @return
     */
    public TextView getPositiveButton() {
        return positiveButton;
    }

    /**
     * 获取左侧按钮
     *
     * @return
     */
    public TextView getNegativeButton() {
        return negativeButton;
    }

    /**
     * 获取关闭按钮
     *
     * @return
     */
    public ImageView getCloseButton() {
        return closeButton;
    }

    @Override
    public void show() {
        super.show();
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        if (type==1) {
            params.width = outMetrics.widthPixels * 9 / 10;
        }if (type==2){
            params.width = outMetrics.widthPixels ;
        }else{
            params.width = outMetrics.widthPixels * 4 / 5;
        }
        getWindow().setGravity(gravity);
        getWindow().setAttributes(params);

    }

    /**
     * 设置negativeButton要显示的信息
     *
     * @param negativeMsg
     */
    public IAlertDialog setNegativeMsg(String negativeMsg) {
        this.negativeMsg = negativeMsg;
        if (negativeButton != null && negativeMsg != null) {
            negativeButton.setText(negativeMsg);
        }
        return this;
    }

    /**
     * 设置positiveButton要显示的信息
     *
     * @param positiveMsg
     */
    public IAlertDialog setPositiveMsg(String positiveMsg) {
        this.positiveMsg = positiveMsg;
        if (positiveButton != null && positiveMsg != null) {
            positiveButton.setText(positiveMsg);
        }
        return this;
    }

    /**
     * 设置提示信息
     *
     * @param message
     */
    public IAlertDialog setMessage(String message) {
        this.message = message;
        if (messageView != null && message != null) {
            messageView.setText(Html.fromHtml(message));
        }
        return this;
    }

    /**
     * 设置提示信息
     *
     * @param message
     */
    public IAlertDialog setMessageOutofHtml(String message) {
        this.message = message;
        if (messageView != null && message != null) {
            messageView.setText(message);
        }
        return this;
    }

    /**
     * 设置确定键的响应接口
     *
     * @param positiveOnClickListener
     */
    public IAlertDialog setPositiveOnClickListener(OnClickListener positiveOnClickListener) {
        this.positiveOnClickListener = positiveOnClickListener;
        return this;
    }

    /**
     * 设置取消键的回调接口
     *
     * @param negativeOnClickListener
     */
    public IAlertDialog setNegativeOnClickListener(OnClickListener negativeOnClickListener) {
        this.negativeOnClickListener = negativeOnClickListener;
        return this;
    }

}
